rm(list=ls())
# libraries
library(tidyverse)
library(here)
library(cregg)
library(broom)
library(stargazer)
library(ggrepel)

# rep file path
rep_path = "r/replication"

df = read.csv("gt_muni_crime_2018.csv")


# tests
testmatrix = matrix(, nrow = 3, ncol = 4)
testmatrix[1,1] <- "Variable"
testmatrix[1,2] <- "Survey Sites"
testmatrix[1,3] <- "Non-Survey Sites"
testmatrix[1,4] <- "P-Value for Diff."
testmatrix[2,1] <- "Avg. Homicide Rate (2018)"
testmatrix[3,1] <- "Avg. Extortion Rate (2018)"
testmatrix

homicides = df %>% 
  select(homicide_rate, sample) %>% 
  drop_na() %>% 
  t.test(homicide_rate ~ sample, data = .) 

testmatrix[2,2] <- homicides$estimate[2]
testmatrix[2,3] <- homicides$estimate[1]
testmatrix[2,4] <- homicides$p.value


extortions = df %>% 
  select(extortion2018rate, sample) %>% 
  drop_na() %>% 
  t.test(extortion2018rate ~ sample, data = .) 

testmatrix[3,2] <- extortions$estimate[2]
testmatrix[3,3] <- extortions$estimate[1]
testmatrix[3,4] <- extortions$p.value


# output
dftests = as.data.frame(testmatrix)


stargazer(dftests, title = "Crime in Guatemalan Municipalities", 
          label = "ttest", summary = F, rownames = F, 
          out = here(rep_path, "figures", "muni_sample_diff.tex"))